#pragma once

#include <set>
#include <string>

struct lua_State;

class LuaFile
{
	friend class LuaTable;

	public:
		LuaFile(const std::string& filename, const std::string& accessMode);
		LuaFile(const std::string& chunk);
		virtual ~LuaFile();

		bool Execute();

		const std::string& GetErrorLog() const { return m_szErrorLog; }
		bool IsValid() const { return (L != NULL); }

		LuaTable GetRoot();
		LuaTable SubTableExpr(const std::string& expr);

	private:
		void AddTable(LuaTable* table) { m_Tables.insert(table); }
		void RemoveTable(LuaTable* table) { m_Tables.erase(table); }

	protected:
		std::string m_szErrorLog;

	private:
		std::string m_szFile, m_szAccessMode;
		std::string m_szChunk;

		lua_State* L;
		std::set<LuaTable*> m_Tables;
		int m_RootRef, m_CurRef;

		bool m_bValid;

		//std::set<std::string> m_AccessedFiles;

	private:
		static LuaFile* CURRENT_PARSER;
};
